package com.ruoyi.project.system.book.controller;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.book.domain.Book;
import com.ruoyi.project.system.book.service.IBookService;
import com.ruoyi.project.system.post.domain.Post;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 图书信息操作处理
 *
 * @author fly
 */
@Controller
@RequestMapping("/system/book")
public class BookController extends BaseController {
    @Autowired
    private IBookService bookService;
    private String prefix = "system/book";

    @RequiresPermissions("system:book:view")
    @GetMapping()
    public String toBook()
    {
        return prefix + "/book";
    }

    @RequiresPermissions("system:book:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Book book)
    {
        startPage();
        List<Book> list = bookService.selectBookList(book);
        return getDataTable(list);
    }

    /**
     * 新增图书
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存图书
     */
    @RequiresPermissions("system:book:add")
    @Log(title = "图书管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Book book)
    {
        if (!bookService.checkBookNameUnique(book))
        {
            return error("新增图书'" + book.getBookName() + "'失败，图书名称已存在");
        }
        else if (!bookService.checkBookCodeUnique(book))
        {
            return error("新增图书'" + book.getBookName() + "'失败，图书ISBN编码已存在");
        }
        return toAjax(bookService.insertBook(book));

    }

    @RequiresPermissions("system:post:remove")
    @Log(title = "图书管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(bookService.deleteBookByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 修改图书
     */
    @RequiresPermissions("system:book:edit")
    @GetMapping("/edit/{bookId}")
    public String edit(@PathVariable("bookId") Long bookId, ModelMap mmap)
    {
        mmap.put("book", bookService.selectBookById(bookId));
        return prefix + "/edit";
    }

    /**
     * 修改保存岗位
     */
    @RequiresPermissions("system:book:edit")
    @Log(title = "图书管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Book book)
    {
        if (!bookService.checkBookNameUnique(book))
        {
            return error("修改图书'" + book.getBookName() + "'失败，图书名称已存在");
        }
        else if (!bookService.checkBookCodeUnique(book))
        {
            return error("修改图书'" + book.getBookName() + "'失败，图书编码已存在");
        }
        return toAjax(bookService.updateBook(book));
    }

    /**
     * 校验图书名称
     */
    @PostMapping("/checkBookNameUnique")
    @ResponseBody
    public boolean checkBookNameUnique(Book book)
    {
        return bookService.checkBookNameUnique(book);
    }

    /**
     * 校验图书编码
     */
    @PostMapping("/checkBookCodeUnique")
    @ResponseBody
    public boolean checkBookCodeUnique(Book book)
    {

        return bookService.checkBookCodeUnique(book);
    }


    @Log(title = "图书管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:book:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Book> util = new ExcelUtil<Book>(Book.class);
        List<Book> bookList = util.importExcel(file.getInputStream());
        String message = bookService.importBook(bookList, updateSupport);
        return AjaxResult.success(message);
    }

    @RequiresPermissions("system:book:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<Book> util = new ExcelUtil<Book>(Book.class);
        return util.importTemplateExcel("图书数据");
    }

    @Log(title = "图书管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:book:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Book book)
    {
        List<Book> list = bookService.selectBookList(book);
        ExcelUtil<Book> util = new ExcelUtil<Book>(Book.class);
        return util.exportExcel(list, "图书数据");
    }


    @PostMapping("/account")
    @RequiresPermissions("system:book:account")
    @ResponseBody
    public AjaxResult account()
    {
        List<Map<String,Object>> rs = bookService.accountByStatus();
        if (rs == null || rs.size() == 0) {
            return AjaxResult.error("没有数据");
        }
        return AjaxResult.success(rs);
    }
}
